package com.zpz.framework.zpzoauth.web;

import com.github.pagehelper.PageInfo;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultMsg;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultStatus;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthRequestUtil;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthValidUtil;
import com.zpz.framework.zpzoauth.pojo.vo.GetUserDepartmentVo;
import com.zpz.framework.zpzoauth.pojo.vo.GetUserInfoVo;
import com.zpz.framework.zpzoauth.pojo.vo.ModifyPasswordValidVo;
import com.zpz.framework.zpzoauth.pojo.vo.SelectMemberDetailsVo;
import com.zpz.framework.zpzoauth.service.ZpzOauthFrameUserService;
import com.zpz.framework.zpzoauth.pojo.qo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zhangpanzhi
 * @time 2019-07-09
 * @description 未经本人允许请勿随便改动，尊重劳动
 * */
@Api(tags = "用户授权管理")
@RequestMapping("/zpzOauthUser")
@RestController
public class ZpzOauthUserController {
    private static Logger log = LoggerFactory.getLogger(ZpzOauthUserController.class);
    @Resource
    private ZpzOauthFrameUserService zpzOauthFrameUserService;


    @ApiOperation(value = "接口：获取用户信息（100004）",notes = "返回码：    \n0（获取信息成功-OK）" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "用户编号", paramType = "query", dataType = "string", required = true)
    })
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
    public ZpzOauthResultMsg<GetUserInfoVo> getUserInfo(HttpServletRequest request, @RequestBody GetUserInfoQo getUserInfoQo) throws Exception {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String clientIdLicense = license.get("clientId");
        return zpzOauthFrameUserService.getUserInfo(clientIdLicense,getUserInfoQo.getUserCode());
    }

    @ApiOperation(value = "接口：添加成员（100021）",
            notes = "  \n返回码：0（OK）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "insertMemberQo", value = "参数", paramType = "body", dataType = "InsertMemberQo", required = true),
    })
    @RequestMapping(value = "/insertMember", method = RequestMethod.POST)
    public ZpzOauthResultMsg<String> insertMember(HttpServletRequest request, @RequestBody InsertMemberQo insertMemberQo) throws Exception {
        if (StringUtils.isBlank(insertMemberQo.getClientId())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.CLIENTID_NULL_31054);
        }
        //验证手机号为空
        if (StringUtils.isBlank(insertMemberQo.getPhoneNumber())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_MOBILE_NOT_NULL_30002);
        }
        //验证手机号合法
        if (!ZpzOauthValidUtil.isMobile(insertMemberQo.getPhoneNumber())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_MOBILE_FORMAT_ERROR_30006);
        }
        //验证手机号是否存在
        ZpzOauthResultMsg<Boolean> mobileMsg = zpzOauthFrameUserService.mobileExist(insertMemberQo.getPhoneNumber(),insertMemberQo.getClientId());
        if (mobileMsg.getData()){
            return  new ZpzOauthResultMsg<String>(mobileMsg.getCode(),mobileMsg.getMessage(),mobileMsg.getErrorDesc(),"");
        }

        //验证用户名是否合法
        if (StringUtils.isNotBlank(insertMemberQo.getUserName()) && !ZpzOauthValidUtil.checkName(insertMemberQo.getUserName())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_NAME_ERROR_30028);
        }

        //验证密码为空
        if (StringUtils.isBlank(insertMemberQo.getPassword())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_PASSWORD_NOT_NULL_30003);
        }
        //验证密码格式
        if (!ZpzOauthValidUtil.checkUserPwd(insertMemberQo.getPassword())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_PASSWORD_FORMAT_ERROR_30007);
        }
        //验证用户角色不能为空
        if (StringUtils.isBlank(insertMemberQo.getUserRoleCode())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_ROLE_NOT_NULL_30027);
        }

        return zpzOauthFrameUserService.addMember(request,insertMemberQo);
    }

    @ApiOperation(value = "接口：删除成员（100022）|负责人：",
            notes = "  \n返回码：0（OK）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deleteMemberQo", value = "参数", paramType = "body", dataType = "DeleteMemberQo", required = true),
    })
    @RequestMapping(value = "/deleteMember", method = RequestMethod.POST)
    public ZpzOauthResultMsg<Boolean> deleteMember(HttpServletRequest request,@RequestBody DeleteMemberQo deleteMemberQo) throws Exception {
        String userCode = deleteMemberQo.getUserCode();
        if(StringUtils.isBlank(userCode))
        {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.MEMBER_ID_NOT_AVAILABLE_30033);
        }
        if (StringUtils.isBlank(deleteMemberQo.getClientId())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.CLIENTID_NULL_31054);
        }
        return zpzOauthFrameUserService.deleteMember(userCode,deleteMemberQo.getClientId());
    }


    @ApiOperation(value = "接口：修改用户状态（禁用、启用）（100023）|负责人：",
            notes = "  \n返回码：0（OK）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updateStatusQo", value = "参数", paramType = "body", dataType = "UpdateStatusQo", required = true),
    })
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public ZpzOauthResultMsg<Boolean> updateStatus(HttpServletRequest request, @RequestBody UpdateStatusQo updateStatusQo) throws Exception {
        String userCode = updateStatusQo.getUserCode();
        if(StringUtils.isBlank(userCode))
        {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.MEMBER_ID_NOT_AVAILABLE_30033);
        }
        if (StringUtils.isBlank(updateStatusQo.getClientId())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.CLIENTID_NULL_31054);
        }
        EditStatusQo qo=new EditStatusQo();
        qo.setClientId(updateStatusQo.getClientId());
        qo.setStatus(updateStatusQo.getStatus());
        qo.setUserCode(userCode);
        return zpzOauthFrameUserService.editStatus(qo);
    }
    @ApiOperation(value = "接口：修改用户状态（禁用、启用）（100023）|负责人：",
            notes = "  \n返回码：0（OK）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updateStatusQo", value = "参数", paramType = "body", dataType = "UpdateStatusQo", required = true),
    })
    @RequestMapping(value = "/editStatus", method = RequestMethod.POST)
    public ZpzOauthResultMsg<Boolean> editStatus(HttpServletRequest request, @RequestBody UpdateStatusQo updateStatusQo) throws Exception {
        return zpzOauthFrameUserService.editStatus(updateStatusQo.getUserCode(),updateStatusQo.getStatus());
    }

    @ApiOperation(value = "接口：用户详情（100024）|负责人：",
            notes = "  \n返回码：0（OK）" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "selectMemberDetailsQo", value = "参数", paramType = "body", dataType = "SelectMemberDetailsQo", required = true),
    })
    @RequestMapping(value = "/selectMemberDetails", method = RequestMethod.POST)
    public ZpzOauthResultMsg<SelectMemberDetailsVo> selectMemberDetails(HttpServletRequest request,
                                                                        @RequestBody SelectMemberDetailsQo selectMemberDetailsQo) throws Exception {
        if (!StringUtils.isNotBlank(selectMemberDetailsQo.getUserCode())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.MEMBER_CODE_NOT_NULL_30036);
        }
        if (StringUtils.isBlank(selectMemberDetailsQo.getClientId())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.CLIENTID_NULL_31054);
        }
        return zpzOauthFrameUserService.selectMemberDetails(selectMemberDetailsQo,selectMemberDetailsQo.getClientId());
    }

    @ApiOperation(value = "接口：成员分页信息（100025）|负责人：",
            notes = "  \n返回码：0（OK）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSelectMemberDetailsQo", value = "参数", paramType = "body", dataType = "PageSelectMemberDetailsQo", required = true),
    })
    @RequestMapping(value = "/pageSelectMemberDetails", method = RequestMethod.POST)
    public ZpzOauthResultMsg<PageInfo<SelectMemberDetailsVo>> pageSelectMemberDetails(HttpServletRequest request,
                                                                              @RequestBody PageSelectMemberDetailsQo pageSelectMemberDetailsQo) throws Exception {
        if(StringUtils.isBlank(pageSelectMemberDetailsQo.getType())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.CLIENTID_NULL_31054);
        }else{
            pageSelectMemberDetailsQo.setType(pageSelectMemberDetailsQo.getType());
        }
        return zpzOauthFrameUserService.pageSelectMemberDetails(pageSelectMemberDetailsQo);
    }

    @ApiOperation(value = "接口：成员分页信息(部门控制权限下)（100063）|负责人：",
            notes = "  \n返回码：0（OK）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "getUserDepartmentQo", value = "参数", paramType = "body", dataType = "GetUserDepartmentQo", required = true),
    })
    @RequestMapping(value = "/getUserDepartment", method = RequestMethod.POST)
    public ZpzOauthResultMsg<PageInfo<GetUserDepartmentVo>> getUserDepartment(@RequestBody GetUserDepartmentQo getUserDepartmentQo) throws Exception {
        return zpzOauthFrameUserService.getUserDepartment(getUserDepartmentQo);
    }

    @ApiOperation(value = "接口：成员信息修改（100026）|负责人：",
            notes = "  \n返回码：0（OK）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "updateMemberInfoQo", value = "参数", paramType = "body", dataType = "UpdateMemberInfoQo", required = true),
    })
    @RequestMapping(value = "/updateMemberInfo", method = RequestMethod.POST)
    public ZpzOauthResultMsg<Boolean> updateMemberInfo (HttpServletRequest request,@RequestBody UpdateMemberInfoQo updateMemberInfoQo)
    {
        String userCode = updateMemberInfoQo.getUserCode();
        if(StringUtils.isBlank(userCode))
        {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.MEMBER_ID_NOT_AVAILABLE_30033);
        }
        if (StringUtils.isBlank(updateMemberInfoQo.getClientId())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.CLIENTID_NULL_31054);
        }
        return zpzOauthFrameUserService.updateMemberInfo(request,updateMemberInfoQo);
    }

    @ApiOperation(value = "接口：修改密码验证接口（100027）  |" ,notes =
            "返回码：")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "modifyPasswordValidQo", value = "修改密码验证接口", paramType = "body", dataType = "ModifyPasswordValidQo", required = true),
    })
    @RequestMapping(value = "/modifypasswordvalid", method = RequestMethod.POST)
    public ZpzOauthResultMsg<ModifyPasswordValidVo> modifyPasswordValid(HttpServletRequest request, @RequestBody ModifyPasswordValidQo modifyPasswordValidQo) throws Exception {
        // 验证手机号非空
        if (!StringUtils.isNotBlank(modifyPasswordValidQo.getPhoneNumber())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_MOBILE_NOT_NULL_30002);
        }
        //验证验证码非空
        if (!StringUtils.isNotBlank(modifyPasswordValidQo.getShortMsgCode())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_VALID_CODE_NOT_NULL_30004);
        }
        //验证手机格式
        if (!ZpzOauthValidUtil.isMobile(modifyPasswordValidQo.getPhoneNumber())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_MOBILE_FORMAT_ERROR_30006);
        }
        return zpzOauthFrameUserService.modifyPasswordValid(modifyPasswordValidQo);
    }

    @ApiOperation(value = "接口：修改密码接口（100031）" ,notes =
            "返回码：  \n" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modifyPasswordByPwQo", value = "修改密码接口参数", paramType = "body", dataType = "ModifyPasswordByPwQo", required = true),
    })
    @RequestMapping(value = "/modifypasswordbyoldpw", method = RequestMethod.POST)
    public ZpzOauthResultMsg<Boolean> modifyPasswordByOldPw(HttpServletRequest request, @RequestBody ModifyPasswordByPwQo modifyPasswordByPwQo) throws Exception {
        //验证旧密码非空
        if (!StringUtils.isNotBlank(modifyPasswordByPwQo.getOldPassword())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_PASSWORD_NOT_NULL_30003);
        }
        //验证密码非空
        if (!StringUtils.isNotBlank(modifyPasswordByPwQo.getPassword())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_PASSWORD_NOT_NULL_30003);
        }

        //验证旧密码格式
        if (!ZpzOauthValidUtil.checkUserPwd(modifyPasswordByPwQo.getOldPassword())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_PASSWORD_FORMAT_ERROR_30007);
        }
        //验证密码格式
        if (!ZpzOauthValidUtil.checkUserPwd(modifyPasswordByPwQo.getPassword())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_PASSWORD_FORMAT_ERROR_30007);
        }
        return zpzOauthFrameUserService.modifyPasswordByOldPw(request,modifyPasswordByPwQo);
    }

    @ApiOperation(value = "接口：修改手机号码接口（100032）" ,notes =
            "返回码：  \n" +
            "  \n0（密码修改成功-OK）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modifyPhoneNumberQo", value = "修改手机号码接口参数", paramType = "body", dataType = "ModifyPhoneNumberQo", required = true),
    })
    @RequestMapping(value = "/modifyphonenumber", method = RequestMethod.POST)
    public ZpzOauthResultMsg<Boolean> modifyPhoneNumber(HttpServletRequest request, @RequestBody ModifyPhoneNumberQo modifyPhoneNumberQo) throws Exception {
        // 验证手机号非空
        if (!StringUtils.isNotBlank(modifyPhoneNumberQo.getPhoneNumber())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_MOBILE_NOT_NULL_30002);
        }
        //验证验证码非空
        if (!StringUtils.isNotBlank(modifyPhoneNumberQo.getValidCode())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_VALID_CODE_NOT_NULL_30004);
        }
        //验证手机格式
        if (!ZpzOauthValidUtil.isMobile(modifyPhoneNumberQo.getPhoneNumber())){
            return  ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_MOBILE_FORMAT_ERROR_30006);
        }
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String clientIdLicense = license.get("clientId");
        if (StringUtils.isBlank(clientIdLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10026);
        }
        return zpzOauthFrameUserService.modifyPhoneNumber(request,modifyPhoneNumberQo);
    }


    @ApiOperation(value = "接口：首次登录修改初始密码接口（100056） ", notes =
            "返回码：  \n")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modifyPasswordByFirstLoginQo", value = "首次登录修改初始密码接口参数", paramType = "body", dataType = "ModifyPasswordByFirstLoginQo", required = true),
    })
    @RequestMapping(value = "/modifyPasswordByFirstLogin", method = RequestMethod.POST)
    public ZpzOauthResultMsg<Boolean> modifyPasswordByFirstLogin(HttpServletRequest request, @RequestBody ModifyPasswordByFirstLoginQo modifyPasswordByFirstLoginQo) throws Exception {
        //验证密码非空
        if (!StringUtils.isNotBlank(modifyPasswordByFirstLoginQo.getPassword())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_PASSWORD_NOT_NULL_30003);
        }
        //验证密码格式 至少6-15位，支持数字、字母、标点符号
        if (!ZpzOauthValidUtil.checkPassword(modifyPasswordByFirstLoginQo.getPassword())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_PASSWORD_FORMAT_ERROR_30007);
        }

        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license == null || license.size() == 0) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        if (StringUtils.isNotBlank(modifyPasswordByFirstLoginQo.getAginPassword())){
            if (modifyPasswordByFirstLoginQo.getAginPassword().equals(modifyPasswordByFirstLoginQo.getPassword())){
                return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.USER_PASSWORD_NOT_NULL_30011);
            }
        }
        return zpzOauthFrameUserService.modifyPasswordByFirstLogin(license, modifyPasswordByFirstLoginQo);
    }

    @ApiOperation(value = "接口：校验该用户是否首次登录（100057） ", notes =
            "返回码： true为首次登录，false为非首次登录")
    @RequestMapping(value = "/checkIsFirstLogin", method = RequestMethod.GET)
    public ZpzOauthResultMsg<Boolean> checkIsFirstLogin(HttpServletRequest request) throws Exception {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        String clientId = license.get("clientId");
        if (license == null || StringUtils.isBlank(clientId)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.OAUTH_10004);
        }
        return zpzOauthFrameUserService.checkIsFirstLogin(license);
    }
}
